#!/bin/sh

set -e
set -u

PATH='/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin'
PATH="$HOME/bin:$HOME/opt/erlang/bin:$PATH"
PATH="$HOME/.cache/rebar3/bin:$HOME/.mix/escripts:$PATH"

export PATH

rel_name='eturnal'
rel_vsn="$(tools/get-version | sed 's/+.*//')"
prefix="/home/$rel_name"
web_root_dir="$prefix/web"
deb_repo_dir="/repositories/$rel_name/deb"
rpm_repo_dir="/repositories/$rel_name/rpm"
dl_dir="$web_root_dir/download"
dl_linux_dir="$dl_dir/linux"
dl_linux_pkg_dir="$dl_linux_dir/package"
dl_linux_run_dir="$dl_linux_dir/installer"
user_doc_dir="$web_root_dir/doc"
api_doc_dir="$web_root_dir/doc/code"
debs="${rel_name}_$rel_vsn-1_amd64.deb ${rel_name}_$rel_vsn-1_arm64.deb"
rpms="${rel_name}-$rel_vsn-1.x86_64.rpm ${rel_name}-$rel_vsn-1.aarch64.rpm"
upstream_name='processone'
main_branch='master'
domain="$rel_name.net"
ip_addr=$(host "$domain" | awk '/has address/ {print $NF}')
myself=${0##*/}

if [ $# != 0 ]
then
	echo >&2 "Usage: $0"
	exit 2
fi

if [ "$(id -un)" != "$rel_name" ] || ! ip addr show | grep -Fq "$ip_addr"
then
	echo >&2 "The $0 tool expects the environment of $rel_name@$domain."
	echo >&2 'Aborting.'
	exit 1
fi

echo "$myself: Going to publish $rel_name $rel_vsn in a few seconds."
echo "$myself: Press <Ctrl>+<C> to abort."
sleep 5

# Push release into Git repository.
git push "$upstream_name" "$main_branch" "$rel_vsn"

# Copy tarballs and packages to web site.
mv "$rel_name-$rel_vsn.tar.gz" "$dl_dir"
mv "$rel_name-$rel_vsn"*'.tar.gz' "$dl_linux_dir"
mv "$rel_name-$rel_vsn"*'.run' "$dl_linux_run_dir"
mv "$rel_name"*'.deb' "$dl_linux_pkg_dir"
mv "$rel_name"*'.rpm' "$dl_linux_pkg_dir"

# Copy documentation to web site.
mv 'index.html' "$user_doc_dir"
rebar3 as exdoc edoc
rm -rf "$api_doc_dir"
mv 'exdoc' "$api_doc_dir"

# Update DEB repository.
for deb in $debs
do
	reprepro -b "$deb_repo_dir" includedeb stable "$dl_linux_pkg_dir/$deb"
done

# Update RPM repository.
rm -f "$rpm_repo_dir/"*'.rpm'
for rpm in $rpms
do
	cp -p "$dl_linux_pkg_dir/$rpm" "$rpm_repo_dir"
	rpm --addsign "$rpm_repo_dir/$rpm"
done
createrepo_c "$rpm_repo_dir"
gpg --detach-sign --armor "$rpm_repo_dir/repodata/repomd.xml"

# Update Homebrew Formula.
tools/update-formula

cat >"$web_root_dir/install" <<-EOF
	#!/bin/sh
	set -e
	set -u
	rel_name='$rel_name'
	rel_vsn='$rel_vsn'
	os='linux'
	platform="\$(uname -s)/\$(uname -m)"
	domain="\$rel_name.net"
	tmp_dir=\$(mktemp -d "\$rel_name.XXXXXX")
	trap 'rm -rf "\$tmp_dir"' INT TERM EXIT
	unsupported()
	{
	    echo >&2 "No \$rel_name binaries available for \$1 systems."
	    echo >&2
	    echo >&2 "For building \$rel_name from source, see:"
	    echo >&2
	    echo >&2 "https://\$domain/doc/install.html"
	    exit 1
	}
	case \$platform in
	Linux/aarch64) arch='arm64' ;;
	Linux/x86_64) arch='x64' ;;
	*) unsupported "\$platform" ;;
	esac
	if ldd '/bin/ls' | grep 'libc' | grep -q 'musl'
	then libc='musl'
	else libc='glibc'
	fi
	installer="\$rel_name-\$rel_vsn-\$os-\$libc-\$arch.run"
	url="https://\$domain/\$installer"
	cd "\$tmp_dir"
	if type curl >'/dev/null'
	then curl -fsSLO "\$url"
	else wget --quiet "\$url"
	fi
	chmod +x "\$installer"
	./\$installer --quiet --nox11
	cd "\$OLDPWD"
EOF
cat >"$web_root_dir/uninstall" <<-EOF
	#!/bin/sh
	set -e
	set -u
	rel_name='$rel_name'
	if [ -e '/run/systemd/system' ] &&
	    systemctl list-unit-files \$rel_name.service >'/dev/null' 2>&1
	then
	    systemctl --quiet --now disable \$rel_name
	    rm -f /etc/systemd/system/\$rel_name.service
	fi
	rm -rf "/opt/\$rel_name"
	if type 'userdel' >'/dev/null'
	then userdel "\$rel_name"
	else deluser "\$rel_name"
	fi
	echo "\$rel_name has been uninstalled successfully."
EOF
